package com.hmall.order.web;

import com.hmall.order.pojo.Order;
import com.hmall.order.service.IOrderService;
import com.hmall.order.web.request.RequestParams;
import com.hmall.order.web.response.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService orderService;


    /**
     * 根据id查询订单
     *
     * @param orderId 订单id
     * @return 统一返回前端结果
     */
    @GetMapping("{id}")
    public OrderVO queryOrderById(@PathVariable("id") Long orderId) {
        // 查询订单
        Order order = orderService.getById(orderId);
        OrderVO orderVO = new OrderVO();
        // 封装到VO
        BeanUtils.copyProperties(order, orderVO);
        // 转化为bigDecimal
        orderVO.setTotalFee(BigDecimal.valueOf(order.getTotalFee()));
        // 返回
        return orderVO;
    }


    /**
     * 新增订单
     *
     * @param params 前端请求参数
     * @return 订单id
     */
    @PostMapping
    public Long saveOrder(@RequestBody RequestParams params) {
        return orderService.saveOrder(params);
    }
}
